import Pagination from "@/components/pagination";
import {
  Message
} from "element-ui";
const easydate = require("@/untils/easydate");
export default {
  components: {
    Pagination
  },
  props: ["activeName"],
  data() {
    return {
      turnPageObj: {},
      checkApply: false,
      searchLoading: false,
      total: 10,
      checkStatus:true,
      agree: false,
      disagree: false,
      infoTitle: '',
      getApprovalInfo: {},
      tableData: [],
      deletIds: [],
      getDeptNameList: [], // 获取部门
      getRankNameList: [], // 获取职级
      rankName: "",
      deptName: "",
      status: "",
    };
  },
  mounted() {
    let that = this;
    let params = {
      rankName: "",
      deptName: "",
      status: "",
      pageNum: 1,
      pageSize: 10
    };
    that.getApproval(params);
    that.$get('getDeptName').then(res => {
      if (res.success) {
        that.getDeptNameList = res.data;
      }
    })
    that.$get('getRankName').then(res => {
      if (res.success) {
        that.getRankNameList = res.data;
      }
    })
  },
  methods: {
    uploadPg() {
      Message({
        message: "敬请期待",
        type: "warning",
        duration: 2000
      });
    },
    handleSelectionChange(e) {
      let that = this;
      let idArr = [];
      e.map(function (item) {
        idArr.push(item.id);
      });
      that.deletIds = idArr;
    },
    pageNum(prop) {
      this.searchList(prop, 10);
    },
    pageSizeNum(prop) {
      this.searchList(1, prop);
    },
    initData(row, column) {
      return row.status == 1 ? "待审批" : row.status == 2 ? "晋级成功" : "晋级失败";
    },
    handleClose(done) {
      done();
    },
    searchList(pageNum, pageSize) {
      let {
        rankName,
        deptName,
        status
      } = this;
      let params = {
        rankName,
        deptName,
        status,
        pageNum,
        pageSize
      };
      this.searchLoading = true;
      this.getApproval(params);
      this.turnPageObj = params;
    },
    deleteClick(row) {
      this.$confirm("此操作将删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = {
          ids: row.id
        }
        this.$delete('deleteApproval', params).then(res => {
          if (res.success) {
            Message({
              message: "操作成功",
              type: "success",
              duration: 2000
            });
            this.searchList(1, 10);
          }
        })
      });
    },
    applyClick(row,status) {
      let that = this;
      console.log(row);
      let params = {
        id: row.id
      };
      that.infoTitle = '当前查看对象：' + row.username;
      that.$get("getApprovalInfo", params, true).then(res => {
        if (res.success) {
          that.checkApply = true;
          if(status){
            that.checkStatus = false;
          }else{
            that.checkStatus = true;
          }
          for (var key in res.data) {
            if (key == "hiredDate") {
              res.data.hiredDate = res.data.hiredDate.split(' ')[0];
            }
            if (key == "detentionTime") {
              res.data.detentionTime = res.data.detentionTime + '年';
            }
            if (key == "eduKind") {
              let eduKind = res.data[key];
              if (eduKind == 0) {
                res.data.eduKind = '大专'
              } else if (eduKind == 1) {
                res.data.eduKind = '本科'
              } else if (eduKind == 2) {
                res.data.eduKind = '硕士'
              } else if (eduKind == 3) {
                res.data.eduKind = '博士'
              } else {
                res.data.eduKind = '其它'
              }
            }
          }
          that.getApprovalInfo = res.data;
        }
      });
    },
    apply(status) {
      let {
        getApprovalInfo
      } = this;
      let params = {
        body: [{
          "id": getApprovalInfo.id,
          "userId": getApprovalInfo.userId,
          "rankId": getApprovalInfo.nextRankId,
          "status": ''
        }]
      };
      if (status == 0) {
        this.disagree = true;
        params.body[0].status = 3;
      } else {
        this.agree = true;
        params.body[0].status = 2;
      }
      this.$post("updateApproval", params).then(res => {
        if (res.success) {
          Message({
            message: "操作成功",
            type: "success",
            duration: 2000
          });
          if (status == 0) {
            this.disagree = false;
          } else {
            this.agree = false;
          }
          this.checkApply = false;
          this.searchList(1, 10);
        } else {
          if (status == 0) {
            this.disagree = false;
          } else {
            this.agree = false;
          }
        }
      });
    },
    getApproval(params) {
      this.$get("getApproval", params).then(res => {
        if (res.success) {
          let isObjNull = JSON.stringify(res.data);
          if (isObjNull !== "{}") {
            this.tableData = res.data.rankApprovals;
            this.total = res.data.total;
          }
          this.searchLoading = false;
        } else {
          this.searchLoading = false;
        }
      });
    }
  }
};
